package com.emep.changzhi.analyse.http.basics;

import com.emep.changzhi.analyse.entity.basics.SysRegionInfo;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.basics.SysRegionInfoService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author xy
 * @Description: 流域控制器
 * @date 2018/2/24 17:20
 * @version V1.0
 */

@RestController
@RequestMapping("region")
@Api(value="流域controller",tags={"流域操作接口"})
public class SysRegionInfoController {
    @Autowired
    private SysRegionInfoService regionInfoService;

    /**
     * 新增流域
     * @param payload
     * @return
     */
    @ApiOperation(value="新增流域", notes="新增流域")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "修改时传的id", required = false, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "regionName", value = "流域名称", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "regionCode", value = "系统层级码", required = true, dataType = "String",paramType="query")
    })
    @PostMapping(value = "add")
    public ResultBean<SysRegionInfo> add(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "regionName","regionCode");
        if(result.getCode() == 1){
            return result.boolAdd(regionInfoService.save(payload)) ;

        }
        return  result;
    }
    /**
     * 根据id 删除流域
     * @param payload
     */
    @ApiOperation(value="删除流域", notes="根据id删除流域")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "流域id", required = true, dataType = "String",paramType="query"),
    })
    @DeleteMapping(value = "/delete")
    public ResultBean delete(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == 1){
           return result.bool(regionInfoService.deleteRegion(payload.get("id").toString()));
        }
        return result;
    }

    /**
     * 查询所有流域
     * @return
     */
    @ApiOperation(value="查询所有流域", notes="无参数查询流域")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @GetMapping(value = "list")
    public ResultBean<List<SysRegionInfo>> queryRegion(){
        return new ResultBean<List<SysRegionInfo>>(regionInfoService.findAllRegions());
    }

    /**
     * 通过id 根据id查询流域
     * @param payload
     * @return
     */
    @ApiOperation(value="根据id查询流域", notes="根据id查询流域")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "流域id", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping(value = "info")
    public ResultBean<SysRegionInfo> findAreaById(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == 1) {
            return result.ok(regionInfoService.findRegionById(payload.get("id").toString()));
        }
        return result;
    }

    /**
     * 分页查询流域
     * page，第几页，从0开始，默认为第0页
     * size，每一页的大小，默认为20
     * sort，排序相关的信息，以property,property(,ASC|DESC)的方式组织，例如sort=firstname&sort=lastname,desc表示在按firstname正序排列基础上按lastname倒序排列
     * @param
     * @return
     */
    @ApiOperation(value="分页查询流域列表", notes="分页无参数查询流域列表")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "数量", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping("paging")
    public ResultBean<Page<SysRegionInfo>> showRegions(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex","pageSize");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            Page<SysRegionInfo> pageRegionInfo = regionInfoService.findAll(Integer.parseInt(payload.get("pageIndex")
                    .toString()),Integer.parseInt(payload.get("pageSize").toString()));
            return result.ok(pageRegionInfo);
        }
        return result;
    }

    /**
     * 分页查询流域
     * page，第几页，从0开始，默认为第0页
     * size，每一页的大小，默认为20
     * sort，排序相关的信息，以property,property(,ASC|DESC)的方式组织，例如sort=firstname&sort=lastname,desc表示在按firstname正序排列基础上按lastname倒序排列
     * @param
     * @return
     */
    @ApiOperation(value="分页带参数查询流域", notes="分页带参数查询流域")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "数量", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "regionName", value = "流域名称", required = false, dataType = "String",paramType="query")
    })
    @GetMapping("paging/query")
    public ResultBean<Page<SysRegionInfo>> PageRegions(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex","pageSize");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            SysRegionInfo regionInfo = new SysRegionInfo();
            regionInfo.setRegionName(payload.containsKey("regionName") ?payload.get("regionName") != null ? payload.get("regionName").toString():"":"" );
            Page<SysRegionInfo> pageRegionName = regionInfoService.findBookCriteria(Integer.parseInt(payload.get("pageIndex") .toString()),Integer.parseInt(payload.get("pageSize").toString()),regionInfo);
            return result.ok(pageRegionName);
        }
        return result;
    }
}
